package cn.iocoder.yudao.module.picturebook.controller.admin.readingrecord;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;


import javax.validation.*;
import javax.servlet.http.*;
import java.time.LocalDate;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.picturebook.controller.admin.readingrecord.vo.*;
import cn.iocoder.yudao.module.picturebook.dal.dataobject.readingrecord.ReadingRecordDO;
import cn.iocoder.yudao.module.picturebook.service.readingrecord.ReadingRecordService;

@Tag(name = "绘本系统 - 用户阅读记录")
@RestController
@RequestMapping("/picturebook/reading-record")
@Validated
public class ReadingRecordController {

    @Resource
    private ReadingRecordService readingRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建用户阅读记录")
    @PreAuthorize("@ss.hasPermission('picturebook:reading-record:create')")
    public CommonResult<Long> createReadingRecord(@Valid @RequestBody ReadingRecordSaveReqVO createReqVO) {
        // 插入之前判断这个记录是否存在如果存在则是跟新时间
        //根据里面传来的userId和 bookId来判断
        ReadingRecordDO readingRecord = readingRecordService.getReadingRecord(createReqVO.getUserId(),createReqVO.getBookId());
        if(readingRecord != null){
            //将更新时间跟新
            LocalDate now = LocalDate.now();
            readingRecord.setReadDate(now);
            //插入
            readingRecordService.updateReadingRecord(readingRecord);
            return success(readingRecord.getId());
        }
        return success(readingRecordService.createReadingRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户阅读记录")
    @PreAuthorize("@ss.hasPermission('picturebook:reading-record:update')")
    public CommonResult<Boolean> updateReadingRecord(@Valid @RequestBody ReadingRecordSaveReqVO updateReqVO) {
        readingRecordService.updateReadingRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户阅读记录")
    @PreAuthorize("@ss.hasPermission('picturebook:reading-record:delete')")
    public CommonResult<Boolean> deleteReadingRecord(@RequestParam("BookId") Long BookId,@RequestHeader("token") Long userId) {
        //根据用户id和书籍id删除这本书的浏览记录
        readingRecordService.deleteFavorites(BookId,userId);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除用户阅读记录")
                @PreAuthorize("@ss.hasPermission('picturebook:reading-record:delete')")
    public CommonResult<Boolean> deleteReadingRecordList(@RequestParam("ids") List<Long> ids) {
        readingRecordService.deleteReadingRecordListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户阅读记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('picturebook:reading-record:query')")
    public CommonResult<ReadingRecordRespVO> getReadingRecord(@RequestParam("id") Long id) {
        ReadingRecordDO readingRecord = readingRecordService.getReadingRecord(id);
        return success(BeanUtils.toBean(readingRecord, ReadingRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户阅读记录分页")
    @PreAuthorize("@ss.hasPermission('picturebook:reading-record:query')")
    public CommonResult<PageResult<ReadingRecordRespVO>> getReadingRecordPage(@RequestHeader("token") Long userId,@Valid ReadingRecordPageReqVO pageReqVO) {
        pageReqVO.setUserId(userId);
        return success(readingRecordService.getReadingRecordPage(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户阅读记录 Excel")
    @PreAuthorize("@ss.hasPermission('picturebook:reading-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportReadingRecordExcel(@Valid ReadingRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ReadingRecordRespVO> list = readingRecordService.getReadingRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户阅读记录.xls", "数据", ReadingRecordRespVO.class,
                        BeanUtils.toBean(list, ReadingRecordRespVO.class));
    }
    //获取当天和这个这周的阅读浏览记录分别书的总数
    @GetMapping("/get-reading-record-count")
    @Operation(summary = "获取当天和这个这周的阅读浏览记录分别书的总数")
    public CommonResult<String> getReadingRecordCount(@RequestHeader("token") Long userId) {
        return CommonResult.success(readingRecordService.getReadingRecordCount(userId));
    }
    //阅读数据获取
    @GetMapping("/get-reading-record-data")
    @Operation(summary = "获取阅读数据")
    public CommonResult<ReadIngRecordDataRespVO> getReadingRecordData(@RequestHeader("token") Long userId) {
        ReadIngRecordDataRespVO data = readingRecordService.getReadingRecordData(userId);
        return CommonResult.success(data);
    }
}